package com.cskaoyan.proxy;

import com.cskaoyan.bean.IRay;
import com.cskaoyan.bean.Ray;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 生成jdk动态代理的字节码文件的案例
 * @author stone
 * @date 2021/11/17 09:55
 */
public class JdkProxyGeneratorDemo {

    public static void main(String[] args) {
        //jdk动态代理的字节码文件会生成在working directory目录下的com.sun.proxy
        //key从ProxyGenerator
        System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
        Ray ray = new Ray();
        IRay proxy = (IRay) Proxy.newProxyInstance(Ray.class.getClassLoader(), Ray.class.getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Object invoke = method.invoke(ray, args);
                System.out.println("买个卤蛋");
                return invoke;
            }
        });
        proxy.sayGoodbye("lirui", 20, true);
    }
}
